using UnityEngine;

namespace NodeCanvas.DialogueTrees
{
	[AddComponentMenu("")]
	[Name("Dialogue")]
	[Category("Nested")]
	[Description("Execute a nested Dialogue Tree. When that nested Dialogue Tree is finished, this node will continue instead if it has a connection. The Actor selected will be used as 'Owner' for the nested Dialogue Tree.\nUseful for making reusable and contained Dialogue Trees.")]
	[Icon("Dialogue")]
	public class DLGNestedDLG : DLGNodeBase, INestedNode
	{
		[SerializeField]
		private DialogueTree _nestedDLG;

		private DialogueTree nestedDLG
		{
			get
			{
				return _nestedDLG;
			}
			set
			{
				_nestedDLG = value;
			}
		}

		public Graph nestedGraph
		{
			get
			{
				return nestedDLG;
			}
			set
			{
				nestedDLG = (DialogueTree)value;
			}
		}

		public override string nodeName
		{
			get
			{
				return "#" + base.ID + " DIALOGUE";
			}
		}

		protected override Status OnExecute()
		{
			if (!nestedDLG)
			{
				base.DLGTree.StopGraph();
				return Error("No Nested Dialogue Tree assigned!");
			}
			base.DLGTree.currentNode = this;
			CopyActors();
			nestedDLG.StartGraph(base.finalActor, base.finalBlackboard, base.Continue);
			return Status.Running;
		}

		public override void OnGraphStoped()
		{
			if ((bool)nestedDLG)
			{
				nestedDLG.StopGraph();
			}
		}

		public override void OnGraphPaused()
		{
			if ((bool)nestedDLG)
			{
				nestedDLG.PauseGraph();
			}
		}

		private void CopyActors()
		{
			foreach (string dialogueActorName in base.DLGTree.dialogueActorNames)
			{
				if (!nestedDLG.dialogueActorNames.Contains(dialogueActorName))
				{
					nestedDLG.dialogueActorNames.Add(dialogueActorName);
				}
			}
		}
	}
}
